<template>
  <div>
    <n-bar></n-bar>
    <!-- 地址新增的表单
      下载省市区域数据
      npm i @vant/area-data
      -->
    <van-address-edit
      :area-list="areaList"
      show-postal
      show-set-default
      show-delete
      @delete="del"
      :address-info="info"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
    >
      <template #default>
        <div>
          <van-radio-group direction="horizontal" v-model="radio">
            <van-radio name="家" checked-color="#ee0a24">家</van-radio>
            <van-radio name="学校" checked-color="#ee0a24">学校</van-radio>
            <van-radio name="公司" checked-color="#ee0a24">公司</van-radio>
          </van-radio-group>
        </div>
      </template>
    </van-address-edit>
  </div>
</template>
 
<script>
import { areaList } from "@vant/area-data";
import { Addressadd, Addressedit,Addressremove } from "../request/app";
export default {
  data() {
    return {
      areaList,
      radio: this.$route.query.id ? this.$route.query.radio : "家",
      info: {
        ...this.$route.query,
        isDefault: this.$route.query.isDefault == "ture" ? true : false,
      },
    };
  },
  methods: {
    //   封装一个保存的方法
    onSave(e) {
      //     console.log(e,"表单");
      if (this.$route.query.id) {
        // 如果id有就是编辑
        Addressedit({
          id: this.$route.query.id,
          uid: sessionStorage.getItem("userinfo")
            ? JSON.parse(sessionStorage.getItem("userinfo")).uid
            : null,
          username: e.name,
          userphone: e.tel,
          regioncode: e.areaCode,
          location: e.province + e.city + e.county,
          useraddress: e.addressDetail,
          status: e.isDefault ? 1 : 0, //1是默认地址，0不是，
          label: this.radio,
        }).then((res) => {
          if (res.code == 200) {
            console.log(res, "响应");
            this.$router.push("/address");
          }
        });
      } else {
        // 如果没id是添加
        Addressadd({
          uid: sessionStorage.getItem("userinfo")
            ? JSON.parse(sessionStorage.getItem("userinfo")).uid
            : null,
          username: e.name,
          userphone: e.tel,
          regioncode: e.areaCode,
          location: e.province + e.city + e.county,
          useraddress: e.addressDetail,
          status: e.isDefault ? 1 : 0, //1是默认地址，0不是，
          label: this.radio,
        }).then((res) => {
          if (res.code == 200) {
            console.log(res, "响应");
            this.$router.push("/address");
          }
        });
      }
    },

    // 封装一个删除事件
    del() {
      Addressremove({
        id:this.$route.query.id
      }).then(res=>{
        console.log(res,"删除");
        this.$router.push('/address')
      })
    },
  },
  mounted() {},
};
</script>

<style scoped>
</style>
